combinatorics *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

//you can use greater and lee_equal on the place of less
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define fastio()       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define M              1000000007
#define int            long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define max_heap       priority_queue <int>
#define min_heap       priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

void print2d(vector<vector<int>> v)
{
    int n = v.size();
    int m = v[0].size();
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
}  

//------------------------------------------------------------------------------->get hash function
int get_hash(string s)
{
    int h = 0;
    for(char c : s)
    {
        h = (h * 31 + (c - 'a' + 1)) % M;
    }
    return h;
}
int get_hash2(string s)
{
    int h = 0;
    int p_power = 1;
    int p = 31;
    for (char c : s)
    {
        h = (h + (c - 'a' + 1) * p_power) % M;
        p_power = (p_power * p) % M;
    }
    return h;
}
//*****END

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr (names + 1, ',');
	cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
int ans;
int arr[9];
int n , k;
bool check(int n ,int cnt)
{
	if(cnt > k) return false;
	if(n == 1) return true;

	return check(arr[n] ,cnt+1);
}
void dfs(int x)
{
	if(x > k)
	{
		bool flag = 0;
		for(int i=1; i<=k; i++)
		{
			if(check(arr[i] , 0) == false)
			{
				flag =1;
				break;
			}
		}
		if(flag == 0)
		{
			// print(arr);
		 	ans++;
		}
		return;
	}

	for(int i=1; i<=k; i++)
	{
		arr[x] = i;
		dfs(x+1);
	}

}

void solveit() 
{
	cin>>n>>k;

	dfs(1);
	// cout<<ans<<endl;

	for(int i=k+1; i<=n; i++)
	{
		ans = (ans * (n-k))%M;
	}
	cout<<ans<<endl;
	
	
}

int32_t main()
{
	fastio(); 

#ifndef ONLINE_JUDGE
	freopen("input.txt",  "r",  stdin);
	freopen("output.txt", "w", stdout);
#endif

	clock_t z = clock();
	//createsieve();
	int t = 1;
	// cin >> t;
	while (t--) solveit();

	cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
	return 0;
}


Comments

Submit
0 Comments
More Questions

137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence